﻿using Se.Model.Host;
using Se.Model.Data;
using Se.Model.GMCommand;
using Se.Net.Sockets;
using SeServer.Module;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Se.Model.GameModel.Character;
using SeServer.Module.Character;

namespace SeServer
{
    public static class GameWorld
    {
        static GameWorld()
        {
            GameWorld.ServerId = int.Parse(System.Configuration.ConfigurationManager.AppSettings["ServerId"]);
        }

        public static SocketHost Host { get; private set; }

        public static int ServerId { get; private set; }

        /// <summary>
        /// 模块管理器
        /// </summary>
        public static GameControllerManager ModuleManager { get; private set; }

        /// <summary>
        /// 初始化
        /// </summary>
        public static void Initilize()
        {
            Console.Title = "SeServer:" + GameWorld.ServerId;

            DataEntityFactory.Initilize(SqlType.SQLServer, ConfigurationManager.AppSettings["Con"]);

            GameWorld.ModuleManager = new GameControllerManager();
            GameWorld.ModuleManager.Initilize();
        }

        /// <summary>
        /// 开始游戏
        /// </summary>
        public static void Begin(SocketSettings setting)
        {
            var controller = new ProtocolController();
            GameWorld.Host = new SocketHost(new SocketListener(setting), controller);
            GameWorld.Host.Start();
        }

        #region Common Methods

        /// <summary>
        /// 获取玩家
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static Player GetPlayer(long id)
        {
            var playerController = (PlayerController)ModuleManager[ControllerType.Player];
            var player = playerController.GetPlayer(id);
            return player;
        }

        /// <summary>
        /// 获取玩家
        /// </summary>
        /// <param name="playerName"></param>
        /// <returns></returns>
        public static Player GetPlayer(string playerName)
        {
            var playerController = (PlayerController)ModuleManager[ControllerType.Player];
            var player = playerController.GetPlayer(playerName);
            return player;
        }

        #endregion
    }
}
